package org.medicine.action;

import com.opensymphony.xwork2.ModelDriven;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.medicine.model.Medicine;
import org.medicine.service.MedicineService;
import org.medicine.util.ChangeTime;
import org.medicine.util.ExcelUtil;
import org.medicine.vo.FileVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;


@Component("file")
@Scope("prototype")
public class FileAction extends MyActionSupport implements ModelDriven<FileVO> {

    @Qualifier("excelUtil")
    @Autowired
    private ExcelUtil excelUtil;
    //当前登录用户
    private ChangeTime changeTime = new ChangeTime();
    private HttpServletRequest request = ServletActionContext.getRequest();
    private final FileVO fileVO = new FileVO();
    private MedicineService medicineService;
    private final static Logger logger = Logger.getLogger(FileAction.class);
    private List<Object> obj = new ArrayList<Object>();
    private List<Medicine> mes = new ArrayList<Medicine>();
    private static String saveExcelPathDirByMedicine = "/file/medicine/";


    public String createExcelByMedicine() {

        /** 得到保存目录的真实路径 **/
        String saveFilePathDir = request.getSession().getServletContext().getRealPath(saveExcelPathDirByMedicine);
        // 得到备份文件的目录的真实路径
        File dir = new File(saveFilePathDir);
        // 如果该目录不存在，就创建
        if (!dir.exists()) {
            dir.mkdirs();
        }
        mes = medicineService.allMedicine();
        if (mes.size() <= 0) {
            fileVO.setResultMessage("药物信息为空！");
            return ERROR;
        }
        String date = changeTime.formatDate(changeTime.getCurrentDate());
        //使用于07以上的版本，03以下的可以修改参数
        excelUtil.exportExcelByPath(saveFilePathDir + "/" + date + ".xlsx",
                mes, Medicine.class, true, date + " 药物信息 ");
        fileVO.setResultMessage("成功备份药物信息文件！文件名为" + date);
        logger.trace("Admin backup room used records success,file name is " + date);
        return SUCCESS;
    }

    public String recoverExcelByMedicine() {
        try {
            //** 得到保存目录的真实路径 **//*
            String saveFilePathDir = request.getSession().getServletContext().getRealPath(saveExcelPathDirByMedicine);
            obj = excelUtil.readExcelByPath(saveFilePathDir + "/" + fileVO.getExcelFileName(),
                    Medicine.class, 1, 0);
            for (int i = 0; i < obj.size(); i++) {
                mes.add((Medicine) obj.get(i));
            }
            for (Medicine medicine : mes) {
                medicine.setId(0);
                medicineService.save(medicine);
            }
        } catch (NullPointerException e) {
            fileVO.setResultMessage("要读取的Excel文件的格式不正确，请检查");
            return ERROR;
        }
        fileVO.setResultMessage("成功还原药物信息，请查看数据库。");
        logger.trace(" Admin recover the room used records success");
        return SUCCESS;
    }

    @Resource(name = "medicineServiceImpl")
    public void setMedicineService(MedicineService medicineService) {
        this.medicineService = medicineService;
    }


    @Override
    public FileVO getModel() {
        return fileVO;
    }

}
